package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.*;
import com.example.demo.mapper.DrugMapper;
import com.example.demo.mapper.FacilityMapper;
import com.example.demo.mapper.PeopleMapper;
import com.example.demo.mapper.TreatMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController

@RequestMapping(value ={"/student","/user"})
@CrossOrigin
public class Rules4Controller {
    @Resource
    PeopleMapper peopleMapper;

    @Resource
    DrugMapper drugMapper;

    @Resource
    TreatMapper treatMapper;

    @Resource
    FacilityMapper facilityMapper;

    @PostMapping("/apply1")
    public Map<String, String> apply(@RequestBody String info) {
        System.out.println(info);
        JSONObject sjsoninfo = JSONObject.parseObject(info);
        String name = sjsoninfo.getString("name");
        String dname = sjsoninfo.getString("dname");
        String tname = sjsoninfo.getString("tname");
        String fname = sjsoninfo.getString("fname");
        int id = sjsoninfo.getIntValue("id");
        int dtq = sjsoninfo.getIntValue("dta");
        int ttq = sjsoninfo.getIntValue("ttq");
        int ftq = sjsoninfo.getIntValue("ftq");
//药品的
        QueryWrapper<Drug> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dname", dname);
        Drug drug1 = drugMapper.selectOne(queryWrapper);
        int claim1 = drug1.getDclaim();
        Double fee1 = drug1.getDprice();

        QueryWrapper<Treat> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("tname", tname);
        Treat treat1 = treatMapper.selectOne(queryWrapper2);
        int claim2 = treat1.getTclaim();
        Double fee2 = treat1.getTprice();

        QueryWrapper<Facility> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("fname", fname);
        Facility facility1 = facilityMapper.selectOne(queryWrapper3);
        int claim3 = facility1.getFclaim();
        Double fee3 = facility1.getFprice();
        Double sum = fee1*claim1*dtq + fee2*claim2*ttq + fee3*claim3*ftq;

        People people= peopleMapper.selectOne(new QueryWrapper<People>().eq("id", id));
        Double total =people.getTotal();
        Double annual =people.getAnnual();
        Double actualpay = total-sum;
        annual+=actualpay;

        if (people != null) {
            people.setClaimd(sum);
            people.setActualpay(actualpay);
            people.setAnnual(annual);
            peopleMapper.updateById(people);
        }


        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("申请状态", "success");
        return resultMap;

    }
}
